package com.ieasy.module.oa.project.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.util.UUIDHexGenerator;
import com.ieasy.module.common.service.BaseService;
import com.ieasy.module.oa.project.entity.ComapnyMemberCount;
import com.ieasy.module.oa.project.web.form.ComapnyMemberCountForm;

@Service
public class ComapnyMemberCountService extends BaseService implements IComapnyMemberCountService {
	
	@Inject
	private IBaseDao<ComapnyMemberCount> basedaoComapnyMemberCount;
	
	@Override
	public Msg addOrUpdate(ComapnyMemberCountForm form) {
		Msg msg = new Msg() ;
		
		//检查该用户是否存在（年份，人员ID）
		List<ComapnyMemberCount> checkPerson = this.basedaoComapnyMemberCount.list("select t from ComapnyMemberCount t where t.year=?", new Object[]{form.getYear()}) ;
		
		
		//存在这修改数据
		if(null != checkPerson && checkPerson.size() > 0) {
			String sql1 = "update ieasy_oa_company_member_count set "+form.getMonth()+"=? where year=? and category='guangzhou'" ;
			this.basedaoComapnyMemberCount.executeSQL(sql1, new Object[]{form.getGuangzhou(), form.getYear()}) ;
			
			String sql2 = "update ieasy_oa_company_member_count set "+form.getMonth()+"=? where year=? and category='suzhou'" ;
			this.basedaoComapnyMemberCount.executeSQL(sql2, new Object[]{form.getSuzhou(), form.getYear()}) ;
			
			String sql3 = "update ieasy_oa_company_member_count set "+form.getMonth()+"=? where year=? and category='beijin'" ;
			this.basedaoComapnyMemberCount.executeSQL(sql3, new Object[]{form.getBeijin(), form.getYear()}) ;
		} else {//不存在这创建
			
			String sql1 = "insert into ieasy_oa_company_member_count(id, year, "+form.getMonth()+", category) values(?,?,?,'guangzhou')" ;
			this.basedaoComapnyMemberCount.executeSQL(sql1, new Object[]{UUIDHexGenerator.generator(), form.getYear(), form.getGuangzhou()}) ;
			
			String sql2 = "insert into ieasy_oa_company_member_count(id, year, "+form.getMonth()+", category) values(?,?,?,'suzhou')" ;
			this.basedaoComapnyMemberCount.executeSQL(sql2, new Object[]{UUIDHexGenerator.generator(), form.getYear(), form.getSuzhou()}) ;
			
			String sql3 = "insert into ieasy_oa_company_member_count(id, year, "+form.getMonth()+", category) values(?,?,?,'beijin')" ;
			this.basedaoComapnyMemberCount.executeSQL(sql3, new Object[]{UUIDHexGenerator.generator(), form.getYear(), form.getBeijin()}) ;
		}
		
		
		msg.setMsg("提交成功");
		msg.setStatus(true);
		return msg;
	}
	
	@Override
	public DataGrid dagagrid(ComapnyMemberCountForm form) {
		DataGrid dg = new DataGrid() ;
		
		dg.setRows(this.getComapnyMemberCount(form));
		return dg;
	}
	
	private List<Map<String, Object>> getComapnyMemberCount(ComapnyMemberCountForm form) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>() ;
		
		//个人（部长）显示的数据只有6行
		Map<String, Object> m1 = new HashMap<String, Object>() ;
		m1.put("category", "广州开发在职总人数") ;
		Map<String, Object> m2 = new HashMap<String, Object>() ;
		m2.put("category", "苏州开发在职总人数") ;
		Map<String, Object> m3 = new HashMap<String, Object>() ;
		m3.put("category", "北京开发在职总人数") ;
		
		//获取个人的数据
		List<ComapnyMemberCountForm> ComapnyMemberCountList = this.find(form) ;
		for (ComapnyMemberCountForm p : ComapnyMemberCountList) {
			if(p.getCategory().equals("guangzhou")) {
				m1.put("m1", p.getM1()==null?0:p.getM1());
				m1.put("m2", p.getM2()==null?0:p.getM2());
				m1.put("m3", p.getM3()==null?0:p.getM3());
				m1.put("m4", p.getM4()==null?0:p.getM4());
				m1.put("m5", p.getM5()==null?0:p.getM5());
				m1.put("m6", p.getM6()==null?0:p.getM6());
				m1.put("m7", p.getM7()==null?0:p.getM7());
				m1.put("m8", p.getM8()==null?0:p.getM8());
				m1.put("m9", p.getM9()==null?0:p.getM9());
				m1.put("m10", p.getM10()==null?0:p.getM10());
				m1.put("m11", p.getM11()==null?0:p.getM11());
				m1.put("m12", p.getM12()==null?0:p.getM12());
			}
			if(p.getCategory().equals("suzhou")) {
				m2.put("m1", p.getM1()==null?0:p.getM1());
				m2.put("m2", p.getM2()==null?0:p.getM2());
				m2.put("m3", p.getM3()==null?0:p.getM3());
				m2.put("m4", p.getM4()==null?0:p.getM4());
				m2.put("m5", p.getM5()==null?0:p.getM5());
				m2.put("m6", p.getM6()==null?0:p.getM6());
				m2.put("m7", p.getM7()==null?0:p.getM7());
				m2.put("m8", p.getM8()==null?0:p.getM8());
				m2.put("m9", p.getM9()==null?0:p.getM9());
				m2.put("m10", p.getM10()==null?0:p.getM10());
				m2.put("m11", p.getM11()==null?0:p.getM11());
				m2.put("m12", p.getM12()==null?0:p.getM12());
			}
			if(p.getCategory().equals("beijin")) {
				m3.put("m1", p.getM1()==null?0:p.getM1());
				m3.put("m2", p.getM2()==null?0:p.getM2());
				m3.put("m3", p.getM3()==null?0:p.getM3());
				m3.put("m4", p.getM4()==null?0:p.getM4());
				m3.put("m5", p.getM5()==null?0:p.getM5());
				m3.put("m6", p.getM6()==null?0:p.getM6());
				m3.put("m7", p.getM7()==null?0:p.getM7());
				m3.put("m8", p.getM8()==null?0:p.getM8());
				m3.put("m9", p.getM9()==null?0:p.getM9());
				m3.put("m10", p.getM10()==null?0:p.getM10());
				m3.put("m11", p.getM11()==null?0:p.getM11());
				m3.put("m12", p.getM12()==null?0:p.getM12());
			}
		}
		
		list.add(m1) ;
		list.add(m2) ;
		list.add(m3) ;
		
		return list ;
	}
	
	private List<ComapnyMemberCountForm> find(ComapnyMemberCountForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String sql = "select " +
					 "t.id, t.year, t.category, "+
					 "t.m1, t.m2, t.m3, t.m4, t.m5, t.m6, t.m7, t.m8, t.m9, t.m10, t.m11, t.m12 " +
					 "from ieasy_oa_company_member_count t " +
				 	 "where 1=1 ";
		
		
		sql = addWhere(sql, form, alias) ;
		return this.basedaoComapnyMemberCount.listSQL(sql, alias, ComapnyMemberCountForm.class, false) ;
	}
	
	private String addWhere(String sql, ComapnyMemberCountForm form, Map<String, Object> params) {
		if (null != form) {
			sql += " and t.year =:year";
			params.put("year", form.getSearchYear());
		}
		return sql ;
	}


}
